import {getOrder} from '@/base/api/OrderApi';
import {Page} from '@/base/Page';
import {Order} from '@/base/model/Order';
import {OrderDetail} from '@/base/model/OrderDetail';
import {ReturnDetail} from '@/base/model/ReturnDetail';

export class DetailBloc {
  OrderInfo = new Order();
  ReturnInfo = new ReturnDetail();
  paged1: Page<OrderDetail> = new Page<OrderDetail>([], 0, 1, 10, 0, []);
  paged3: Page<OrderDetail> = new Page<OrderDetail>([], 0, 1, 10, 0, []);

  OrderId = 0;
  loading = false;
  hasFormInfo= false;
  load(id: number) {
    return getOrder({orderId: id})
      .then((res) => {
        // eslint-disable-next-line no-eval
        let data = eval('(' + res.data + ')');
        this.OrderInfo = data.errMsg.Detail;
        this.OrderInfo.OrderStage = this.getStatus(data.errMsg.Detail.OrderStage);
        this.OrderInfo.ConsigneeAddress =
          data.errMsg.Detail.Province + data.errMsg.Detail.City +
          data.errMsg.Detail.Area + data.errMsg.Detail.Address;
        this.paged1.items = data.errMsg.Results;
        this.paged1.itemCount = data.errMsg.Results.length;
        this.paged1.pageCount = Math.ceil(data.errMsg.Results.length / 10);
        if (data.errMsg.ReturnOrder && data.errMsg.ReturnDetail) {
          this.ReturnInfo = data.errMsg.ReturnOrder;
          this.ReturnInfo.Stage = this.getStage(data.errMsg.ReturnOrder.Stage);
          this.paged3.items = data.errMsg.ReturnDetail;
          this.paged3.itemCount = data.errMsg.ReturnDetail.length;
          this.paged3.pageCount = Math.ceil(data.errMsg.ReturnDetail.length / 10);
        }
        return res;
      }).finally(() => {
        this.hasFormInfo = true;
      })
  }
  getStatus(OrderStage) {
    switch (OrderStage) {
      case '1' : return '待发货';
      case '2' : return '待收货';
      case '3' : return '已收货';
      case '4' : return '已取消';
      case '5' : return '仅退款';
      case '6' : return '退货';
      case '7' : return '换货';
      default : return '';
    }
  }
  getStage(Stage) {
    switch (Stage) {
      case '0' : return '退回待审核';
      case '1' : return '退回待发货';
      case '2' : return '退回待收货';
      case '3' : return '退货已完成';
      case '4' : return '退回不通过';
      case '5' : return '仅退款完成';
      case '6' : return '换货待收货';
      case '7' : return '换货已完成';
      default : return '';
    }
  }
}
